package com.lagou.edu.course.impl.remote;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lagou.edu.common.util.ConvertUtils;
import com.lagou.edu.course.api.CourseRemoteService;
import com.lagou.edu.course.api.dto.CourseDTO;
import com.lagou.edu.course.api.param.CourseQueryParam;
import com.lagou.edu.course.impl.entity.po.Course;
import com.lagou.edu.course.impl.service.ICourseService;
import com.lagou.edu.response.ResponseDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseService implements CourseRemoteService {

    @Autowired
    private ICourseService courseService;

    @Override
    public CourseDTO getCourseById(@RequestParam("courseId") Integer courseId) {
        final Course course = courseService.getById(courseId);
        final CourseDTO courseDTO = new CourseDTO();
        BeanUtil.copyProperties(course, courseDTO);
        return courseDTO;
    }
    
    @Override
    public ResponseDTO updateCourseStatusById(final Integer courseId, final Integer status) {
        Course course = new Course();
        course.setId(courseId);
        course.setStatus(status);
        ResponseDTO responseDTO = null;
        try {
            courseService.updateById(course);
            responseDTO = ResponseDTO.success();
        } catch (Exception e) {
            responseDTO = ResponseDTO.ofError(e.getMessage());
            e.printStackTrace();
        }
        return responseDTO;
    }
    
    @Override
    public ResponseDTO saveOrUpdateCourse(final CourseDTO courseDTO) {
        final Course course = ConvertUtils.convert(courseDTO, Course.class);
        System.out.println(courseDTO.getCreateTime());
        // 新增课程
        if (course.getId() == null) {
            course.setCreateTime(LocalDateTime.now());
            course.setUpdateTime(LocalDateTime.now());
        } else {  // 修改课程
            course.setUpdateTime(LocalDateTime.now());
        }
        ResponseDTO responseDTO = null;
        try {
            courseService.saveOrUpdate(course);
            responseDTO = ResponseDTO.success();
        } catch (Exception e) {
            responseDTO = ResponseDTO.ofError(e.getMessage());
            e.getStackTrace();
        }
        return responseDTO;
    }
    
    @Override
    public Page<CourseDTO> getQueryCourses(final CourseQueryParam courseQueryParam) {
        Integer currentPage = courseQueryParam.getCurrentPage();
        Integer pageSize = courseQueryParam.getPageSize();
        String courseName = courseQueryParam.getCourseName();
        Integer status = courseQueryParam.getStatus();
        
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(courseName)) {
            queryWrapper.like("course_name", courseName);
        }
        if (status != null) {
            queryWrapper.eq("status", status);
        }
        // 查询总记录数
        final int count = courseService.count(queryWrapper);
        queryWrapper.orderByDesc("id");
        
        Page<Course> page = new Page<>(currentPage, pageSize == null ? 30 : pageSize);
        final IPage<Course> coursePage = courseService.getBaseMapper().selectPage(page, queryWrapper);
        
        List<CourseDTO> courseDTOList = new ArrayList<>();
        coursePage.getRecords().forEach( course -> {
            final CourseDTO courseDTO = new CourseDTO();
            BeanUtil.copyProperties(course, courseDTO);
            courseDTOList.add(courseDTO);
        });
        
        final Page<CourseDTO> coursePageResultDTO = new Page<>();
        BeanUtil.copyProperties(coursePage, coursePageResultDTO);
        coursePageResultDTO.setRecords(courseDTOList);
        coursePageResultDTO.setTotal(count);  //总记录数需要单独查询
        return coursePageResultDTO;
    }

   
}
